﻿using System;
using System.Linq;
using System.Linq.Dynamic;
using ChoViet.Lib.Paging;
using System.Collections.Generic;

namespace ChoViet.Model
{
    public class ShopManager
    {
        public static IPagedList<Shop> GetListShop(int? demandId, int? areaId, int? cityId, int? districtId, int? custypeId, int? cateId, string keyword, bool activeYn, bool pubYn, string order, string orderDir, int? page, int? pageSize, int? userId)
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            try
            {
                if (eDataContext.Connection.State == System.Data.ConnectionState.Closed) eDataContext.Connection.Open();
                var shops = eDataContext.Shops
                    .Where(a => demandId > 0 ? a.DemandId == demandId : true)
                    //.Where(a => areaId > 0 ? a.AreaID == areaId : true)
                    .Where(a => cityId > 0 ? a.CityId == cityId : true)
                    .Where(a => districtId > 0 ? a.DistricID == districtId : true)
                    //.Where(a => custypeId > 0 ? a.CusTypeID == custypeId : true)
                    .Where(a => cateId > 0 ? (a.CateID == cateId || a.Category.ParentID == cateId) : true)
                    .Where(a => userId.Value > 0 ? a.UserID == userId : true)
                   // .Where(a => activeYn == true ? a.ActiveYN == activeYn : true)
                    .Where(a => pubYn == true ? a.PubYN == pubYn : true)
                    .Where(a => keyword != null && keyword.Length > 0 ? (a.Name.ToLower().Contains(keyword.ToLower()) || (a.ID.ToString().Contains(keyword)) || (a.EmailContact.ToLower().ToString().Contains(keyword.ToLower())) || (a.MobileContact.ToString().Contains(keyword))) : true)
                    .OrderBy(string.Format("{0} {1}", order, orderDir))
                    .ToPagedList(page.Value - 1, pageSize.Value);
                return shops;
            }
            catch (Exception)
            {
                eDataContext.Connection.Dispose();
                return null;
            }
        }
    }
}
